﻿using B.Z.BasicData.API.Read.DTO;
using B.Z.BasicData.Domain.Entity.Authorize;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;

namespace B.Z.BasicData.API.Read.Application.Role
{
    public class UserNameQueryCommandHanler : IRequestHandler<UserNameQueryCommand, APIResult<List<MenberDto>>>
    {
        private readonly IBaseRepository<T_User> userRepository;

        public UserNameQueryCommandHanler(IBaseRepository<T_User> userRepository)
        {
            this.userRepository = userRepository;
        }

        public Task<APIResult<List<MenberDto>>> Handle(UserNameQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MenberDto>> result = new APIResult<List<MenberDto>>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            var list = userRepository.GetAll().Where(p => p.DepartMentId == request.DeprtmentId);
            List<MenberDto> dtolist = new List<MenberDto>();
            foreach (var item in list)
            {
                MenberDto dto = new MenberDto();
                dto.Id = item.Id;
                dto.Label = item.UserName;
                dtolist.Add(dto);
            }
            result.Data = dtolist;
            return Task.FromResult(result);
        }
    }
}
